<?php
// +----------------------------------------------------------------------
// | Fanwe 多语商城建站系统 (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 云淡风轻(97139915@qq.com)
// +----------------------------------------------------------------------

// 友情链接
class LinkAction extends CommonAction{
	//增
	public function add()
	{
		$new_sort = D(MODULE_NAME)-> max("sort") + 1;
		$this->assign('new_sort',$new_sort);
		$this->display();
	}
	
	public function insert() {
		//B('FilterString');
		$name=$this->getActionName();
		$model = D ($name);
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		if($_REQUEST['logo']!="")
		{
			$model->logo = $_REQUEST['logo'];
			$model->type = 2; //有填写图片地址时设置类型
		}
		$upload_list = $this->uploadFile(0,"link");
		if($upload_list)
		{
			$model->logo = $upload_list[0]['recpath'].$upload_list[0]['savename'];
			$model->type = 1; //有上传图片时设置类型
		}
		
		//保存当前数据对象
		$list=$model->add ();
		if ($list!==false) { //保存成功
			$msg = '添加友情链接'.$_REQUEST['name'];	
			$this->saveLog(1,$list);
			$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
			$this->success (L('ADD_SUCCESS'));
		} else {
			//失败提示
			$msg = '添加友情链接'.$_REQUEST['name'];	
			$this->saveLog(0,$list);
			$this->error (L('ADD_FAILED'));
		}
	}
	
	
	public function update() {
		//B('FilterString');
		$origin_link = D("Link")->getById($_REQUEST['id']); //原始的链接数据
		
		$name=$this->getActionName();
		$model = D ( $name );
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		$upload_list = $this->uploadFile(0,"link");
		if($upload_list)
		{
			if($origin_link['type']==1)	@unlink($this->getRealPath().$origin_link['logo']); //原始链接为图片链接时删除原图
			$model->logo = $upload_list[0]['recpath'].$upload_list[0]['savename'];
			$model->type = 1; //有上传图片时设置类型
		}
		else
		{
			//无上传图片时(可能性1: 未改变已有logo值: 未作改变  2:改变logo值且不为空: 变为外链 3:清空logo变为文本)
			if($_REQUEST['logo']!=""&&$origin_link['logo']!=$_REQUEST['logo'])
			{
				$model->logo = $_REQUEST['logo'];
				$model->type = 2; //有填写图片地址时设置类型
				if($origin_link['type']==1)	@unlink($this->getRealPath().$origin_link['logo']); //原始链接为图片链接时删除原图
			}
			elseif($_REQUEST['logo']=="")
			{
				$model->logo =	"";
				$model->type = 0; //有填写图片地址时设置类型
				if($origin_link['type']==1)	@unlink($this->getRealPath().$origin_link['logo']); //原始链接为图片链接时删除原图
			}
			else 
			{
				$model->logo = $origin_link['logo'];
				$model->type = 1;
			}
		}
		
		// 更新数据
		$list=$model->save ();
		if (false !== $list) {
			$msg = '修改友情链接'.$_REQUEST['id'];	
			//成功提示
			$this->saveLog(1,$_REQUEST['id'],$msg);
			$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
			$this->success (L('EDIT_SUCCESS'));
		} else {
			//错误提示
			$msg = '修改友情链接'.$_REQUEST['id'];	
			$this->saveLog(0,$_REQUEST['id'],$msg);
			$this->error (L('EDIT_FAILED'));
		}
	}
	

	public function foreverdelete() {
		//删除指定记录
		$name=$this->getActionName();
		$model = D ($name);
		if (! empty ( $model )) {
			$pk = $model->getPk ();
			$id = $_REQUEST [$pk];
			if (isset ( $id )) {
				$condition = array ($pk => array ('in', explode ( ',', $id ) ) );
				$itemList = D("Link")->where($condition)->findAll();
				if (false !== $model->where ( $condition )->delete ()) {
					//echo $model->getlastsql();\
					//开始删除相应的logo文件
					foreach ($itemList as $item)
					{
						if($item['type']==1)
						{
							@unlink($this->getRealPath().$item['logo']);
						}
					}
					$this->saveLog(1);
					$this->success (L('DEL_SUCCESS'));
				} else {
					$this->saveLog(0);
					$this->error (L('DEL_FAILED'));
				}
			} else {
				$this->saveLog(0);
				$this->error (L('INVALID_OP'));
			}
		}
		$this->forward ();
	}

}
?>